package com.zh.modeapp.views;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zh.mouldleapp.R;

public class MainActivty extends Activity {
	protected ImageView imgv_zero;
	protected ImageView imgv_one;
	protected ImageView imgv_two;
	protected ImageView imgv_three;
	protected ImageView imgv_four;

	protected TextView tv_zero;
	protected TextView tv_one;
	protected TextView tv_two;
	protected TextView tv_three;
	protected TextView tv_four;

	protected LinearLayout ll_zero;
	protected LinearLayout ll_one;
	protected LinearLayout ll_two;
	protected LinearLayout ll_three;
	protected LinearLayout ll_four;

	public final static int ZEROTAB = 0;// 聊天
	public final static int ONETAB = 1;// 通讯录
	public final static int TWOTAB = 2;// 应用
	public final static int THREETAB = 3;// 朋友圈
	public final static int FOURTAB = 4;// 设置

	protected int currentTab = ZEROTAB;
	protected final static int press_color = Color.rgb(0x6f, 0xbe, 0x87);
	protected final static int normal_color = Color.rgb(0xbb, 0xbb, 0xbb);
	protected LinearLayout container;
	private LinearLayout bottomLayout;

	public LocalActivityManager manager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);

		manager = new LocalActivityManager(this, true);
		manager.dispatchCreate(savedInstanceState);

		initComponent();
		initEvent();
		switchActivity(currentTab);

	}

	@Override
	protected void onNewIntent(Intent intent) {
		Bundle bundle = intent.getExtras();
		if (bundle != null && bundle.containsKey("id")) {
			currentTab = bundle.getInt("id");
		} else {
			currentTab = ZEROTAB; // 默认显示的界面
		}
		switchActivity(currentTab);
		super.onNewIntent(intent);
	}

	protected void initComponent() {
		container = (LinearLayout) this.findViewById(R.id.main_frame_layout);

		imgv_zero = (ImageView) this.findViewById(R.id.imgv_zero);
		imgv_three = (ImageView) this.findViewById(R.id.imgv_three);
		imgv_one = (ImageView) this.findViewById(R.id.imgv_one);
		imgv_two = (ImageView) this.findViewById(R.id.imgv_two);
		imgv_four = (ImageView) this.findViewById(R.id.imgv_four);

		ll_zero = (LinearLayout) this.findViewById(R.id.ll_zero);
		ll_three = (LinearLayout) this.findViewById(R.id.ll_three);
		ll_four = (LinearLayout) this.findViewById(R.id.ll_four);
		ll_two = (LinearLayout) this.findViewById(R.id.ll_two);
		ll_one = (LinearLayout) this.findViewById(R.id.ll_one);

		tv_zero = (TextView) this.findViewById(R.id.tv_zero);
		tv_three = (TextView) this.findViewById(R.id.tv_three);
		tv_four = (TextView) this.findViewById(R.id.tv_four);
		tv_one = (TextView) this.findViewById(R.id.tv_one);
		tv_two = (TextView) this.findViewById(R.id.tv_two);

		bottomLayout = (LinearLayout) this.findViewById(R.id.main_foot_layout);
	}

	protected void onStart() {
		super.onStart();

	}

	int backCount = 0;
	long currentTime = 0;

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			return true;
		}

		if (event.getKeyCode() == KeyEvent.KEYCODE_SEARCH) {
			return true;
		}
		return container.getChildAt(0).dispatchKeyEvent(event);
	}

	private OnClickListener footOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ll_one:
			case R.id.imgv_one:
				if (currentTab == ONETAB) {
					return;
				}
				switchActivity(ONETAB);
				break;
			case R.id.ll_three:
			case R.id.imgv_three:
				if (currentTab == THREETAB) {
				}
				switchActivity(THREETAB);
				break;
			case R.id.ll_zero:

			case R.id.imgv_zero:
				switchActivity(ZEROTAB);
				break;
			case R.id.ll_four:
			case R.id.imgv_four:
				if (currentTab == FOURTAB) {
					return;
				}
				switchActivity(FOURTAB);
				break;
			case R.id.imgv_two:
			case R.id.ll_two:
				if (currentTab == TWOTAB) {
					return;
				}
				switchActivity(TWOTAB);
				break;
			}
		}
	};

	public void switchActivity(int id) {
		InputMethodManager imm = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
		if (imm != null && MainActivty.this.getCurrentFocus() != null
				&& MainActivty.this.getCurrentFocus().getWindowToken() != null) {
			imm.hideSoftInputFromWindow(MainActivty.this.getCurrentFocus()
					.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		}
		int OldCurrentTab;
		OldCurrentTab = currentTab;
		currentTab = id;
		container.removeAllViews();
		Intent intent = null;
		String activityName = null;
		switch (id) {
		case ZEROTAB:
			intent = new Intent(MainActivty.this, Tab_Zero_Activity.class);
			activityName = "Tab_Zero_Activity";
			break;
		case ONETAB:
			intent = new Intent(MainActivty.this, Tab_One_Activity.class);
			activityName = "Tab_One_Activity";
			break;
		case TWOTAB:
			intent = new Intent(MainActivty.this, Tab_Two_Activity.class);
			activityName = "Tab_Two_Activity";
			break;
		case THREETAB:
			intent = new Intent(MainActivty.this, Tab_Three_Activity.class);
			activityName = "Tab_Three_Activity";
			break;
		case FOURTAB:
			intent = new Intent(MainActivty.this, Tab_Four_Activity.class);
			activityName = "Tab_Four_Activity";
			break;
		}
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Window subActivity = getLocalManager().startActivity(activityName,
				intent);
		container.addView(subActivity.getDecorView(), LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		resetLinear();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

	}

	// 设置底部背景
	private void resetLinear() {
		imgv_four.setBackgroundResource(R.drawable.main_set_bg);
		imgv_zero.setBackgroundResource(R.drawable.main_chat_bg);
		imgv_three.setBackgroundResource(R.drawable.main_friendcircle_bg);
		imgv_one.setBackgroundResource(R.drawable.main_contacts_bg);
		imgv_two.setBackgroundResource(R.drawable.main_wall_bg);

		ll_zero.setBackgroundResource(R.drawable.main_bottom_layout_bg_normal);
		ll_three.setBackgroundResource(R.drawable.main_bottom_layout_bg_normal);
		ll_four.setBackgroundResource(R.drawable.main_bottom_layout_bg_normal);
		ll_one.setBackgroundResource(R.drawable.main_bottom_layout_bg_normal);
		ll_two.setBackgroundResource(R.drawable.main_bottom_layout_bg_normal);
		tv_zero.setTextColor(normal_color);
		tv_three.setTextColor(normal_color);
		tv_four.setTextColor(normal_color);
		tv_one.setTextColor(normal_color);
		tv_two.setTextColor(normal_color);
		switch (currentTab) {
		case ONETAB:
			ll_one.setBackgroundResource(R.drawable.main_bottom_layout_bg_over);
			imgv_one.setBackgroundResource(R.drawable.main_contacts_over);
			tv_one.setTextColor(press_color);
			break;
		case ZEROTAB:
			ll_zero.setBackgroundResource(R.drawable.main_bottom_layout_bg_over);
			imgv_zero.setBackgroundResource(R.drawable.main_chat_over);
			tv_zero.setTextColor(press_color);
			break;
		case THREETAB:
			ll_three.setBackgroundResource(R.drawable.main_bottom_layout_bg_over);
			imgv_three.setBackgroundResource(R.drawable.main_friendcircle_over);
			tv_three.setTextColor(press_color);
			break;
		case FOURTAB:
			ll_four.setBackgroundResource(R.drawable.main_bottom_layout_bg_over);
			imgv_four.setBackgroundResource(R.drawable.main_set_over);
			tv_four.setTextColor(press_color);
			break;
		case TWOTAB:
			ll_two.setBackgroundResource(R.drawable.main_bottom_layout_bg_over);
			imgv_two.setBackgroundResource(R.drawable.main_app_over);
			tv_two.setTextColor(press_color);
			break;
		}
	}

	protected void initComponentValue() {
		resetLinear();
	}

	protected void initEvent() {
		imgv_zero.setOnClickListener(footOnClickListener);
		ll_zero.setOnClickListener(footOnClickListener);
		imgv_three.setOnClickListener(footOnClickListener);
		ll_three.setOnClickListener(footOnClickListener);
		imgv_four.setOnClickListener(footOnClickListener);
		ll_four.setOnClickListener(footOnClickListener);
		imgv_one.setOnClickListener(footOnClickListener);
		ll_one.setOnClickListener(footOnClickListener);
		imgv_two.setOnClickListener(footOnClickListener);
		ll_two.setOnClickListener(footOnClickListener);
	}

	public void hideBottomLayout() {
		if (bottomLayout != null) {
			bottomLayout.setVisibility(View.GONE);
		}
	}

	public void showBottomLayout() {
		if (bottomLayout != null) {
			bottomLayout.setVisibility(View.VISIBLE);
		}
	}

	public LocalActivityManager getLocalManager() {
		return manager;
	}

}